using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.CommonWeb;
using NTTHAO.Common.BL;
using NTTHAO.Web.BL.CommonPage;

public partial class admin_webparts_Plug_ins_CommonPage_CommonPageMenuInput : System.Web.UI.UserControl
{
    public bool isAdmin = false;
    public string _ReturnUrl = "/admin/CommonPageContent.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave2.Text = this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        this.chkUseURL.Text = LanguageUtility.GetLanguageText("YES", Session);
        this.isAdmin = "admin".Equals(Context.User.Identity.Name, StringComparison.OrdinalIgnoreCase);

        if (Request.QueryString["ReturnUrl"] != null)
        {
            _ReturnUrl = Request.QueryString["ReturnUrl"];
        }
        else
        {
            _ReturnUrl += "?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString();
        }

        if (!IsPostBack)
        {
            this.cmbContentType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("VIEWTYPE_TEXT", Session), "0"));
            this.cmbContentType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("VIEWTYPE_IMAGE", Session), "1"));
            this.cmbContentType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("VIEWTYPE_LINK", Session), "2"));
            this.cmbContentType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("PRODUCT", Session), "3"));

            string action = Request.QueryString["action"];

            if ("editmnu".Equals(action))
            {
                try
                {
                    int mnuid = Int32.Parse(Request.QueryString["mnuid"].Trim());
                    CommonMenu mnu = new CommonMenu(mnuid);

                    if (mnu.ID < 1)
                    {
                        throw new Exception();
                    }

                    this.txtMenuText.Text = mnu.Text;
                    this.txtDescription.Text = mnu.Description;
                    this.chkActive.Checked = mnu.Active;

                    this.cmbContentType.SelectedValue = mnu.LinkType.ToString();
                    this.txtContentURL.Text = mnu.LinkURL;
                    this.cmbContentTarget.SelectedValue = mnu.Target;

                    this.chkUseURL.Checked = mnu.ViewType == 1;
                    this.txtURL.Text = mnu.URL;
                }
                catch (Exception)
                {
                    Response.Redirect("CommonPageContent.aspx?action=invalidurl", false);
                    return;
                }
            }
            else // add new
            {
                try
                {
                    int mnuid = Int32.Parse(Request.QueryString["mnuid"].Trim());
                    //if (mnuid != null)
                    {
                        CommonMenu mnu = new CommonMenu(mnuid);

                        if (mnu.ID < 1)
                        {
                            //throw new Exception();
                        }
                        else
                        {
                            this.cmbContentType.SelectedValue = mnu.LinkType.ToString();
                        }
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("CommonPageContent.aspx?action=invalidurl", false);
                    return;
                }
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CommonMenu mnu = null;

            if ("editmnu".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                int mnuid = Int32.Parse(Request.QueryString["mnuid"].Trim());
                mnu = new CommonMenu(mnuid);

                if (this.isAdmin)
                {
                    mnu.LinkType = chkUseURL.Checked ? 1 : 0;
                    mnu.URL = txtURL.Text.Trim();

                    mnu.ViewType = this.chkUseURL.Checked ? 1 : 0;
                    mnu.URL = this.txtURL.Text.Trim();
                }
            }
            else
            {
                int mnuid = Int32.Parse(Request.QueryString["mnuid"].Trim());
                int groupid = Int32.Parse(Request.QueryString["g"].Trim());

                mnu = new CommonMenu();
                mnu.GroupID = groupid;
                mnu.ParentID = mnuid;

                mnu.ViewType = this.chkUseURL.Checked ? 1 : 0;
                mnu.URL = this.txtURL.Text.Trim();

                // for default
                mnu.RootType = 0;
                mnu.Reserve_01 = "";
                mnu.Reserve_02 = "";
            }

            mnu.Text = txtMenuText.Text.Trim();
            mnu.Description = txtDescription.Text.Trim();
            mnu.Active = chkActive.Checked;

            mnu.LinkType = Convert.ToInt32(this.cmbContentType.SelectedValue);
            mnu.LinkURL = this.txtContentURL.Text;
            mnu.Target = this.cmbContentTarget.SelectedValue;

            if ("editmnu".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                mnu.Update();
            }
            else
            {
                mnu.Insert();
            }

            //Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            Response.Redirect("CommonPageContent.aspx?action=reloadtop", false);
        }
        catch (Exception) 
        {
            Response.Redirect("CommonPageContent.aspx?action=invalidurl", false);
            return;
        }
    }
}
